package com.ruoyi.shop.system.service.impl;

import com.ruoyi.auth.service.SysLoginService;
import com.ruoyi.common.core.constant.CacheConstants;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.JwtUtils;
import com.ruoyi.common.core.utils.ip.IpUtils;
import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.shop.api.domain.Shop;
import com.ruoyi.shop.details.service.IshopService;
import com.ruoyi.shop.system.service.SysShopService;
import com.ruoyi.shop.verification.domain.ShopVerification;
import com.ruoyi.shop.verification.service.IShopVerificationService;
import com.ruoyi.system.api.RemoteRegisterService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Service
public class SysShopServiceImpl implements SysShopService {
    @Autowired
    IshopService shopService;

    @Autowired
    IShopVerificationService shopVerificationService;

    @Autowired
    RemoteRegisterService remoteRegisterService;

    @Autowired
    RedisService redisService;

    @Autowired
    private TokenService tokenService;

    @Override
    public int register(Shop shop, ShopVerification shopVerification) {
        // 检查账号是否重复注册   待做

        SysUser sysUser = new SysUser();
        sysUser.setUserName(shopVerification.getMobile());
        sysUser.setNickName(shop.getName());
        sysUser.setPhonenumber(shopVerification.getMobile());
        sysUser.setAvatar("https://fenglyj.oss-cn-beijing.aliyuncs.com/cf/upload/20250219/PixPin_2025-02-20_17-34-16.png");
        sysUser.setPassword("123456");
        R<Boolean> result = remoteRegisterService.registerUser(sysUser, SecurityConstants.INNER);

        // 如果sys_user表注册失败则直接返回
        if (result.getCode() != 200) {
            return 0;
        }

        // 店铺除了id 字段都可以重复 因此必定插入成功
        shopService.insertshop(shop);

        // 注册店铺认证表  ocr识别审核号码 待做
        shopVerification.setMid(shop.getId());
        shopVerificationService.insertShopVerification(shopVerification);

        return 1;
    }

    @Override
    public AjaxResult login(String moblie, String verifyCode) {
        ShopVerification shopVerification = new ShopVerification();
        shopVerification.setMobile(moblie);

        List<ShopVerification> list = shopVerificationService.selectShopVerificationList(shopVerification);
        // 查找不到该账号
        if (list.isEmpty()) {
            return AjaxResult.error("查无此号");
        }
        shopVerification = list.get(0);
        Shop shop = shopService.selectshopById(shopVerification.getMid());
        if (shop.getAuditStatusId() != 2) {
            return AjaxResult.error("审核中或审核不通过");
        }
        redisService.setCacheObject("shop_id:" + shop.getId(), shop);
        redisService.setCacheObject("shop_verification_id:" + shopVerification.getId(), shopVerification);

        LoginUser userInfo = new LoginUser();
        SysUser sysUser = new SysUser();
        sysUser.setUserName(moblie);
        sysUser.setUserId(199999L);
        userInfo.setSysUser(sysUser);

        Map<String, Object> rspMap = tokenService.createToken(userInfo);
        rspMap.put("shop", shop);
        rspMap.put("shopVerification", shopVerification);
        System.out.println(shop.toString());


        return AjaxResult.success(rspMap);
    }
}
